/**
 * 
 */
package org.xabber.storage.dao;

import java.util.LinkedList;
import java.util.List;

import org.xabber.storage.AbstractAccountTable;
import org.xabber.storage.DataBaseManager;
import org.xabber.storage.dao.row.AccountItem;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * @author dai.rui.lin
 * 
 */
public class AccountTable extends AbstractAccountTable {
	private static final class Fields implements AbstractAccountTable.Fields {

		private Fields() {
		}

		public static final String PASSWROD = "password";

		public static final String TIME = "time";

	}

	private static final String NAME = "Accounts";
	private static final String[] PROJECTION = new String[] { Fields._ID,
			Fields.ACCOUNT, Fields.PASSWROD, Fields.TIME };
	private final DataBaseManager databaseManager;

	private final static AccountTable instance;

	static {
		instance = new AccountTable(DataBaseManager.getInstance());
	}

	public static AccountTable getInstance() {
		return instance;
	}

	public AccountTable(DataBaseManager databaseManager) {
		this.databaseManager = databaseManager;
	}

	@Override
	public void create(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + NAME + " (" + Fields._ID
				+ " INTEGER PRIMARY KEY," + Fields.ACCOUNT + " TEXT,"
				+ Fields.PASSWROD + " TEXT," + Fields.TIME + " TEXT);";
		DataBaseManager.execSQL(db, sql);
	}

	public final long write(Long id, String account, String password,
			String time) {
		ContentValues values = new ContentValues();
		values.put(Fields.ACCOUNT, account);
		values.put(Fields.PASSWROD, password);
		if (time != null) {
			values.put(Fields.TIME, time);
		} else {
			values.putNull(Fields.TIME);
		}
		//
		SQLiteDatabase db = databaseManager.getWritableDatabase();
		if (id == null)
			return db.insert(NAME, Fields.ACCOUNT, values);
		db.update(NAME, values, Fields._ID + " = ?",
				new String[] { String.valueOf(id) });
		return id;
	}

	public final long write(AccountItem item) {
		ContentValues values = new ContentValues();
		values.put(Fields.ACCOUNT, item.Name);
		values.put(Fields.PASSWROD, item.Password);
		values.put(Fields.TIME, item.Time);
		//
		SQLiteDatabase db = databaseManager.getWritableDatabase();
		if (item.id == null)
			return db.insert(NAME, Fields.ACCOUNT, values);
		db.update(NAME, values, Fields._ID + " = ?",
				new String[] { String.valueOf(item.id) });

		return item.id;
	}

	public final void remove(String account) {
		SQLiteDatabase db = databaseManager.getWritableDatabase();
		db.delete(NAME, Fields.ACCOUNT + " = ? ", new String[] { account });
		databaseManager.removeAccount(account);
	}

	public List<AccountItem> readAllData() {
		List<AccountItem> mLm = new LinkedList<AccountItem>();
		Cursor c = list();
		if (hasData(c)) {
			int iid = c.getColumnIndex(Fields._ID);
			int iname = c.getColumnIndex(Fields.ACCOUNT);
			int ipwd = c.getColumnIndex(Fields.PASSWROD);
			int itime = c.getColumnIndex(Fields.TIME);
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				AccountItem mAm = new AccountItem();
				mAm.id = (long) c.getInt(iid);
				mAm.Name = c.getString(iname);
				mAm.Password = c.getString(ipwd);
				mAm.Time = c.getString(itime);
				mLm.add(mAm);
			}
		}
		closeCurosr(c);
		return mLm;
	}

	@Override
	public String getTableName() {
		return NAME;
	}

	@Override
	protected String[] getProjection() {
		return PROJECTION;
	}

}
